<?php

namespace Plugins;

use Phalcon\Events\Event;
use Phalcon\Mvc\User\Plugin;
use Phalcon\Dispatcher;
use Phalcon\Mvc\Dispatcher\Exception as DispatcherException;
use Phalcon\Mvc\Dispatcher as MvcDispatcher;

class NotFound extends Plugin {
    public function beforeException(Event $event, MvcDispatcher $dispatcher, \Exception $exception) {
        if ($exception instanceof DispatcherException) {
            switch ($exception->getCode()) {
                case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward([
                        'namespace'  => 'Controllers',
                        'controller' => 'error',
                        'action'     => 'show404',
                    ]);
                    return false;
            }
        }

        $dispatcher->forward([
            'module'     => 'Controllers',
            'controller' => 'error',
            'action'     => 'show500',
        ]);
        return false;
    }
}
